//
//  LoginDelegate.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/11.
//

import Foundation
import SwiftUI

class ConfigDelegate<T: ConfigProtocol>: ConfigProtocol {
    
    var listRole:[AiRoleBean]? = nil

    private let instance : T
    
    init(instance: T) {
        self.instance = instance
    }
    
    func getFolderConfig() async throws -> FolderConfig{
        return try await instance.getFolderConfig()
    }
    
    func saveFolderConfig(bean: FolderConfig) async throws {
        try await instance.saveFolderConfig(bean: bean)
    }
    
    func createMainFolderDB(folder : CloudFolderBean?) async throws {
        let folderConfig = try await getFolderConfig()
        //主目录为空才去创建
        if ((folderConfig.dbFolderMainId ?? "").isEmpty) {
            //创建数据库目录
            let folderBean = CloudFolderBean(userId: userManager.getUserId())
            folderBean.name = "时光相册"
            let letterNum = getPinyinInitials(folderBean.name)
            folderBean.letterNum = letterNum
            folderBean.time = Int64(Date().timeIntervalSince1970)
            return try await instance.createMainFolderDB(folder: folderBean)
        }
    }
    
    func clearRoleList() {
        listRole = nil
    }
    
    
    //不需要具体是实现该方法，因为在该代理的类就实现了
    func getAiRole(roleId: String?) async throws -> AiRoleBean {
        if (roleId ?? "")?.isEmpty == false {
            let roleBean = try await getAiRoleList().first { $0.objectId == roleId }
            return roleBean ?? AiRoleBean()
        }
        let listSelect = try await getAiRoleList().filter { $0.select }
        //有选中的ai了，则随机一个出来
        if (!listSelect.isEmpty) {
            return listSelect.randomElement() ?? AiRoleBean()
        } else {
            //todo android在这一步有个走旧逻辑，ios就不用了
            return AiRoleBean()
        }
    }
    
    func getSelectAiRoleList() async throws -> [AiRoleBean] {
        return try await getAiRoleList().filter{ $0.select }
    }
    
    func getAiRoleList(_ force: Bool = false) async throws -> [AiRoleBean] {
        if force {
            clearRoleList()
        }
        if listRole != nil {
//            showLog("角色已经加载过了")
            return listRole!
        }
        var list = try await instance.getAiRoleList(force)
        
        let listSystem = try await aiRoleManager.getSystemAiRoleList()
        list.append(contentsOf: listSystem)
        
        let config = try await getFolderConfig()
        list.forEach { bean in
            bean.select = false
            config.aiRoleList?.forEach { select in
                if (select  == bean.objectId ?? "") {
                    bean.select = true
                }
            }
        }
        if list.first(where: {$0.select}) != nil {
            //查找不需要做处理
        } else {
            //查找不到，设置第一个为true
            list.first?.select = true
        }
        listRole = list
        return list
    }
    
    
    func getTagList() async throws -> [TagBean] {
        return try await instance.getTagList()
    }
    
    func addAiRole(bean: AiRoleBean, isAdd: Bool) async throws -> AiRoleBean {
        return try await instance.addAiRole(bean: bean, isAdd: isAdd)
    }
    
    func delAiRole(bean: AiRoleBean) async throws {
        return try await instance.delAiRole(bean: bean)
    }
    
    func selectAiRole(list: [AiRoleBean]) async throws {
        return try await instance.selectAiRole(list: list)
    }
    
    func upload(image: UIImage, name: String) async throws -> CloudPictureBean {
        return try await instance.upload(image: image, name: name)
    }
    
    func upload(url: URL, name: String) async throws -> CloudPictureBean {
        return try await instance.upload(url: url, name: name)
    }
    
    func delUrl(fileId: String) async throws {
        return try await instance.delUrl(fileId: fileId)
    }
    
    func getUploadCapacity() -> Int {
        return instance.getUploadCapacity()
    }
    
    func checkCloudCapacity() -> Bool {
        return instance.checkCloudCapacity()
    }
    
    func setOften(roleId: String?) async throws {
        try await instance.setOften(roleId: roleId)
    }
    
    func getOften() async throws -> String? {
        return try await instance.getOften()
    }
}
